package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.MyActivityInfoVo;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.ItktLog;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

/**
 * 
 * @author wxy
 * @createdate 2014-6-24 下午5:10:57
 * @Description: 我的活动列表的adapter
 */
public class MyActivityAdapter extends BaseAdapter {

	private ArrayList<MyActivityInfoVo> myActivityVo;
	private Context context;
	private final String SPACING=" ";

	public MyActivityAdapter(ArrayList<MyActivityInfoVo> myActivityVo, Context context) {
		super();
		this.myActivityVo = myActivityVo;
		this.context = context;
	}

	@Override
	public int getCount() {
		return myActivityVo.size();
	}

	@Override
	public Object getItem(int position) {
		return myActivityVo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView myActivityItemImgUrl;// 图片的显示
		ImageView myActivityItemIsGone;// 该条活动是否过期
		TextView myActivityItemTitle;// 活动的标题
		TextView myActivityItemDate;// 活动的日期
		TextView myActivityItemExpiryDate;// 活动的有效期
		TextView my_activity_item_expiry_date_text;// 活动中有效期的文字
	}

	ViewHolder viewHolder = null;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.my_activity_item, null);
			viewHolder = new ViewHolder();
			viewHolder.myActivityItemTitle = (TextView) convertView.findViewById(R.id.my_activity_name);
			viewHolder.myActivityItemDate = (TextView) convertView.findViewById(R.id.my_activity_item_date);
			viewHolder.myActivityItemExpiryDate = (TextView) convertView.findViewById(R.id.my_activity_item_expiry_date);
			viewHolder.my_activity_item_expiry_date_text = (TextView) convertView.findViewById(R.id.my_activity_item_expiry_date_text);
			viewHolder.myActivityItemImgUrl = (ImageView) convertView.findViewById(R.id.my_activity_item_img);
			viewHolder.myActivityItemIsGone = (ImageView) convertView.findViewById(R.id.my_activity_item_isgone);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 将传进来的myActivityVo中的imgUrl显示在imageview上
		FinalBitmap fb = FinalBitmap.create(context);
		// if(myActivityVo.get(position).getAct_list_img()!=null||!"".equals(myActivityVo.get(position).getAct_list_img())){
		if (myActivityVo.get(position).getAct_list_img() != null && myActivityVo.get(position).getAct_list_img().length() > 5) {
			ItktLog.e("------" + position + "----" + myActivityVo.get(position).getAct_list_img());
			fb.display(viewHolder.myActivityItemImgUrl, ConnectionUtil.localUrl + myActivityVo.get(position).getAct_list_img());

		}
		if (myActivityVo.get(position).getAct_list_img() != null && myActivityVo.get(position).getAct_list_img().length() < 5) {
			// viewHolder.myActivityItemImgUrl.setBackgroundResource(R.drawable.my_activity_item_img);
			// Bitmap bitmap =
			// BitmapFactory.decodeResource(context.getResources(),
			// R.drawable.my_activity_item_img);
			// fb.display(viewHolder.myActivityItemImgUrl, null, bitmap,
			// bitmap);
			viewHolder.myActivityItemImgUrl.setVisibility(View.GONE);
		}

		// 根据myActivityVo的is_late来判断该活动是否过期 0过期 1没过期, 为0的文字都要置灰
		if ("0".equals(myActivityVo.get(position).getIs_late())) {
			viewHolder.myActivityItemIsGone.setVisibility(View.VISIBLE);
			viewHolder.myActivityItemTitle.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
			viewHolder.myActivityItemDate.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
			viewHolder.myActivityItemExpiryDate.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
			viewHolder.my_activity_item_expiry_date_text.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
		} else {
			viewHolder.myActivityItemIsGone.setVisibility(View.GONE);
			viewHolder.myActivityItemTitle.setTextColor(context.getResources().getColor(R.color.shopping_goodsName_color));
			viewHolder.myActivityItemDate.setTextColor(context.getResources().getColor(R.color.mycoupons_itemdate_color));
			viewHolder.myActivityItemExpiryDate.setTextColor(context.getResources().getColor(R.color.mycoupons_itemdate_color));
			viewHolder.my_activity_item_expiry_date_text.setTextColor(context.getResources().getColor(R.color.mycoupons_itemdate_color));

		}
		// 分别给活动的标题、日期、有效期赋值
		viewHolder.myActivityItemTitle.setText(myActivityVo.get(position).getAct_name());
		// 对返回的日期做处理，返回的格式为2014-7-8 10:26:19，去掉空格
		int col_time_index = myActivityVo.get(position).getCol_time().indexOf(SPACING);
		String col_time = myActivityVo.get(position).getCol_time().substring(0, col_time_index);
		viewHolder.myActivityItemDate.setText(col_time);
		int strat_index = myActivityVo.get(position).getAct_start().indexOf(SPACING);
		String start = myActivityVo.get(position).getAct_start().substring(0, strat_index);
		int end_index = myActivityVo.get(position).getAct_end().indexOf(SPACING);
		String end = myActivityVo.get(position).getAct_end().substring(0, end_index);
		viewHolder.myActivityItemExpiryDate.setText(start + " 至  " + end);
		return convertView;
	}

}
